// 页面加载时初始化
window.onload = function() {
    const usernameInput = document.getElementById('username');
    const passwordInput = document.getElementById('password');
    
    // 清空输入框
    usernameInput.value = '';
    passwordInput.value = '';
    
    // 阻止浏览器自动填充
    usernameInput.setAttribute('readonly', 'readonly');
    passwordInput.setAttribute('readonly', 'readonly');
    
    setTimeout(() => {
        usernameInput.removeAttribute('readonly');
        passwordInput.removeAttribute('readonly');
    }, 500);
};

async function handleLogin(event) {
    event.preventDefault();
    
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    const errorMessage = document.getElementById('errorMessage');
    
    try {
        const response = await fetch('/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                username: username,
                password: password
            })
        });
        
        const data = await response.json();
        
        if (data.success) {
            // 登录成功动画
            document.querySelector('.login-content').style.animation = 'fadeOut 0.5s ease forwards';
            setTimeout(() => {
                window.location.href = `/main?token=${data.data.token}`;
            }, 500);
        } else {
            errorMessage.textContent = data.msg;
            errorMessage.style.display = 'block';
            errorMessage.classList.add('show');
            
            setTimeout(() => {
                errorMessage.classList.remove('show');
                setTimeout(() => {
                    errorMessage.style.display = 'none';
                }, 300);
            }, 3000);
        }
    } catch (error) {
        errorMessage.textContent = '服务器连接失败，请稍后重试';
        errorMessage.style.display = 'block';
        errorMessage.classList.add('show');
    }
    
    return false;
} 